/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */

function ListNode(val, next) {
  this.val = val === undefined ? 0 : val;
  this.next = next === undefined ? null : next;
}

var addTwoNumbers = function (l1, l2) {
  let temp = 0;
  let phead = new ListNode();
  const p = phead;
  while (l1 !== null && l2 !== null) {
    phead.val = (l1.val + l2.val + temp) % 10;
    temp = Math.floor((l1.val + l2.val) / 10);
    l1 = l1.next;
    l2 = l2.next;
    phead.next = new ListNode();
    phead = phead.next;
  }
  return p
};

currentNode1 = {
  val: 2,
  next: {
    val: 4,
    next: {
      val: 3,
      next: null,
    },
  },
};
currentNode2 = {
  val: 5,
  next: {
    val: 6,
    next: {
      val: 4,
      next: null,
    },
  },
};

console.log(addTwoNumbers(currentNode1, currentNode2));
